#include <stack>
#pragma 
#include"Thread.hpp"
#include"Mutex.hpp"
#include"Log.hpp"
#include"Cond.hpp"
#include<vector>
#include<queue>

namespace ThreadPoolModule
{
    using namespace ThreadModlue;
    using namespace MutexModule;
    using namespace CondModule;
    using namespace LogModule;

    static const int gnum = 5;// 默认线程池个数  隐式构造函数
    template<typename T>
    class ThreadPool
    {
    private:
        ThreadPool(int num = gnum)
        :   _num(num),
            _isrunning(false),
            _sleepernum(0)
        {
            for(int i = 0; i < num; i++)
            {
                _threads.emplace_back([this]()
                {
                    HandlerTask();
                });
            }
        }

        void WakeUpAllThread()
        {
            LockGuard lockguard(_mutex);
            if(_sleepernum)
                _cond.Broadcast();
            LOG(LogLevel::INFO)<<"唤醒所有休眠线程";
        }

        void WakeUpOne()
        {
            _cond.Signal();
            LOG(LogLevel::INFO)<<"唤醒一个休眠线程";
        }

        void Start()
        {
            if(_isrunning)
            return ;
            _isrunning = true;
            for(auto& thread:_threads)
            {
                thread.Start();
                LOG(LogLevel::INFO)<<"start new thread success";
            }
        }

        ThreadPool(const ThreadPool<T>&) = delete;
        ThreadPool<T> &operator=(const ThreadPool<T> &) = delete;

    public:
        static ThreadPool<T>* GetInstance()// 单例模式 
        {
            if(inc == nullptr)
            {
                LockGuard lockguard(_lock);
                LOG(LogLevel::DEBUG)<<"获取单例。。。。";
                if(inc == nullptr)
                {
                    LOG(LogLevel::DEBUG) << "首次使用单例, 创建之....";
                    inc = new ThreadPool<T>();
                    inc->Start();
                }
            }
            return inc;
        }


        void Stop()
        {
            if(!_isrunning)
            {
                return ;
            }
            _isrunning = false;
            
        }

        void Join()
        {
            for(auto& thread:_threads)
            {
                thread.Join();
            }
        }

        void HandlerTask()
        {
            char name[128];
            pthread_getname_np(pthread_self(), name, sizeof(name));
            while(1)
            {
                T t;
                {
                    LockGuard lockguard(_mutex);
                    while(_tasks.empty() && _isrunning)//  两种条件同时满足  
                    {
                        _sleepernum++;
                        _cond.Wait(_mutex);
                        _sleepernum--;
                    }
                    if(!_isrunning && _tasks.empty())
                    {
                        LOG(LogLevel::INFO)<<name<<"线程池退出&&任务队列为空";
                        break;
                    }
                    t = _tasks.front();
                    _tasks.pop();
                }
                t();//  处理任务  
            }
        }

        bool Equeue(const T& in)
        {
            if(_isrunning)
            {
                LockGuard lockguard(_mutex);
                _tasks.push(in);
                if(_threads.size() == _sleepernum)
                {
                    WakeUpOne();
                }
                return true;
            }
            return false;
        }

        ~ThreadPool()
        {

        }

    private:
        std::vector<Thread> _threads;
        int _num;
        std::queue<T> _tasks;
        Cond _cond;
        Mutex _mutex;
        bool _isrunning;
        int _sleepernum;


        static ThreadPool<T> * inc;// 单例指针
        static Mutex _lock;//  维护单例线程安全锁
    };

    template <typename T>
    ThreadPool<T> *ThreadPool<T>::inc = nullptr;

    template <typename T>
    Mutex ThreadPool<T>::_lock;
}